/**
 * Project Name: socket-test
 * Package Name: com.meatball
 * File Name: WebSocketSender
 * Date: 2021/1/26 21:52
 * User: 張翔宇
 * Copyright (c) 2021, zhang.xiangyu@foxmail.com All Rights Reserved.
 */
package com.meatball;

import org.springframework.web.reactive.socket.WebSocketMessage;
import org.springframework.web.reactive.socket.WebSocketSession;
import reactor.core.publisher.FluxSink;

/**
 * WebSocketSender 是我们自己创建的类，目的是保存 WebSocket
 * 连接的 session 以及对应的 FluxSink，以便在 WebSocketHandler
 * 代码范围外发送数据
 * @title: WebSocketSender.java
 * @author: 張翔宇
 * @date: 2021/1/26 21:52
 * @version: V3.0
 */
public class WebSocketSender {
    private WebSocketSession session;
    private FluxSink<WebSocketMessage> sink;

    public WebSocketSender(WebSocketSession session, FluxSink<WebSocketMessage> sink) {
        this.session = session;
        this.sink = sink;
    }

    public void sendData(String data) {
        sink.next(session.textMessage(data));
    }
}
